package cmsoa.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmsoa.db.DBUtil;
import cmsoa.db.pojo.TXtUser;
import cmsoa.helper.RequestHelper;
import cmsoa.util.MD5Utils;

import com.opensymphony.xwork2.Action;

public class LoginAction {
	private String code;
	private TXtUser user;
	private boolean success;
	private String msg;
	
	/**
	 * 用户退出登录 清理session
	 * @return
	 */
	public String userOut(){
		RequestHelper.getSession().removeAttribute("user");
		return Action.SUCCESS; 
	}
	
	/**
	 * 用户登录验证
	 * @return
	 */
	public String checkUser(){
		DBUtil db = DBUtil.getDBUtilByRequest();
		Map params = new HashMap(1);
		params.put("UId", user.getUId());
		List list = db.queryByPojo(TXtUser.class, params);
		if(list!=null && list.size()!=0){
			TXtUser dbuser = (TXtUser)list.get(0);
			if(user.getUId().equals(dbuser.getUId()) && user.getUPwd().equals(dbuser.getUPwd())){
				user.setUName(dbuser.getUName());
				user.setURemark(dbuser.getURemark());
				user.setUPwd("");
				RequestHelper.getSession().setAttribute("user", user);
				this.success = true;
			}else{
				this.msg = "用户名或密码错误";
			}
		}else{
			this.msg = "不存在该用户";
		}
		return Action.SUCCESS; 
	}
	
	
	public String getMsg() {
		return msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public TXtUser getUser() {
		return user;
	}
	public void setUser(TXtUser user) {
		this.user = user;
	}
	
}
